package me.cabbit.digitone.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileNotFoundException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;

import me.cabbit.digitone.Application;
import me.cabbit.digitone.support.Strings;
import me.cabbit.digitone.tones.Screentone;

/**
 * This class creates the window and sets up some callbacks. It does not
 * interact directly with the screentones, only handles the interface itself
 * The screentones are handled in the inherited class {@link ScreentoneMainWindow} 
 * 
 * @author Arkaid
 *
 */
public abstract class MainWindow extends JFrame implements WindowListener {
	private static final long serialVersionUID = -2325147763767828171L;

	//------------------------------------------------------------------//
	//--------------------------- properties ---------------------------//
	//------------------------------------------------------------------//
	
	/** Strings used for internationalization */
	protected Strings strings;
	
	/** Menu */
	protected MainWindowMenu menu;
	
	/** Left(Toolbox) <-> Right(Display) panel */
	protected JSplitPane splitPane;
	
	/** Toolbox */
	protected MainWindowToolbox toolbox;
	
	/** Display */
	//protected SVGDisplay display;
	protected ScreentoneDisplay display; 
	
	/** Status bar*/
	protected MainWindowStatus statusBar;
	
	//------------------------------------------------------------------//
	//----------------------------- methods ----------------------------//
	//------------------------------------------------------------------//
	
	/** 
	 * Constructor. Build up the window and set up some basic listeners / handlers
	 */
	public MainWindow() 
	{
		//load strings
		try 
		{
			this.strings = new Strings("lang/MainWindow.strings");
		} 
		catch (FileNotFoundException e) 
		{
			JOptionPane.showMessageDialog(this, "Failed to load strings file", "Unexpected error", JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
		
		//set size, title and center in the middle of screen
		this.setSize(800, 600);
		this.setTitle("Digitone (v" + Application.VERSION_STRING + ")");
		Dimension screenSize = 	Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screenSize.width - 800) / 2;
		int y = (screenSize.height - 600) / 2;
		this.setLocation(x, y);
		
		//Create menu
		this.menu = new MainWindowMenu(this);
		this.add(this.menu, BorderLayout.NORTH);
		
		//Create a split pane to separate the toolbox and the display 
		this.splitPane = new JSplitPane();
		this.splitPane.setDividerSize(3);
		this.splitPane.setDividerLocation((int)(this.getWidth() * 0.3));
		this.add(this.splitPane, BorderLayout.CENTER);
		
		//Create toolbox
		this.toolbox = new MainWindowToolbox(this);
		this.splitPane.setLeftComponent(this.toolbox);
		
		//Create display
		this.display = new ScreentoneDisplay(this);
		this.splitPane.setRightComponent(this.display);
		
		//Create status bar
		this.statusBar = new MainWindowStatus(this);
		this.add(this.statusBar, BorderLayout.SOUTH);
		
		//add listener and display
		this.addWindowListener(this);
		this.setVisible(true);
	}


	/**
	 * Called when "File->Exit" on the menu is selected
	 */
	protected void onMenuFileExit() 
	{
		this.dispose();
	}
	
	/**
	 * Called when "Help->Help Contents" on the menu is selected
	 */
	protected void onMenuHelp()
	{
		JOptionPane.showMessageDialog(this, "I.O.U. 1 help contents");
	}
	
	/**
	 * Called when "Help->About" on the menu is selected
	 */
	protected void onMenuAbout()
	{
		AboutDialog about = new AboutDialog(this);
		about.setVisible(true);
	}
	
	//------------------------------------------------------------------//
	//------------------------ abstract methods ------------------------//
	//------------------------------------------------------------------//

	/**
	 * Called when the "Save preset" button is pressed
	 */
	protected abstract void onPresetSave();

	/**
	 * Called when the "Delete preset" button is pressed
	 */
	protected abstract void onPresetDelete();
	
	/**
	 * Called when a new preset is selected from the combo box. 
	 * 
	 * @param preset Information regarding the selected preset
	 */
	protected abstract void onPresetSelected(Preset preset); 
	
	/**
	 * Called when a new screentone is selected from the combo box. 
	 * 
	 * @param st newly selected screentone
	 */
	protected abstract void onScreentoneSelected(Screentone st);

	/**
	 * Called when "File->Save PNG" on the menu is selected
	 */
	protected abstract void onMenuSavePNG(); 
	
	
			
	//------------------------------------------------------------------//
	//-------------------------- WindowListener ------------------------//
	//------------------------------------------------------------------//
	
	@Override
	public void windowActivated(WindowEvent e) {}
	@Override
	public void windowClosed(WindowEvent e) {}
	@Override
	public void windowClosing(WindowEvent e) {
		this.dispose();
	}
	@Override
	public void windowDeactivated(WindowEvent e) {}
	@Override
	public void windowDeiconified(WindowEvent e) {}
	@Override
	public void windowIconified(WindowEvent e) {}
	@Override
	public void windowOpened(WindowEvent e) {}

}
